package Test;

import java.util.Arrays;

public class Sort {
    public static void main(String[] args) {
        int[] arr = new int[]{1,3,5,2,4,6};
        insertSort2(arr);
        System.out.println(Arrays.toString(arr));
    }
    //1.1直接插入排序
    static void insertSort(int[] arr){
        for(int i = 1 ;i<arr.length;i++){
            int j = i - 1;
            for(;j>=0;j--){
                if(arr[j+1] < arr[j]){
                    int temp = arr[j+1];
                    arr[j+1] =arr[j];
                    arr[j] = temp;
                }else{
                    break;
                }
            }
        }
    }
    static void insertSort2(int[] arr){
        for (int i = 1;i<arr.length;i++){
            int temp = arr[i];
            int j = i - 1;
            for(;j>=0;j--){
                if(arr[j] > temp){
                    arr[j+1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j+1] = temp;
        }
    }
}
